
package com.feib.soeasy.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.feib.soeasy.job.CreateReportJob;
import com.feib.soeasy.job.importor.ImportJob;
import com.feib.soeasy.job.importor.PyerImportJob;
import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.model.User;
import com.feib.soeasy.service.AsyncService;
import com.feib.soeasy.util.SpringContextUtil;


/**
 * @title (#)AsyncServiceImpl.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/3 create by Jimmy Liu<br>
 */
@Async
@Service("asyncService")
public class AsyncServiceImpl extends BaseServiceImpl implements AsyncService {

    @Override    
    public void doFileImport(Long uploadFilePk)
    {
        ImportJob importJob = (ImportJob)SpringContextUtil.getBean("importJob");
        
        Date start = new Date();
        logger.info("帳單整批匯入開始 -" + ImportJob.BASIC_DATE_TIME_FORMAT.format(start));
        try {
            importJob.execute(uploadFilePk);           
        }
        catch (Exception e) {
            ImportJob.logger.error(e.getMessage(), e);
            try {
                importJob.exceptionNotice(e, uploadFilePk);
            }
            catch (IOException e1) {
                ImportJob.logger.error("完全無法發送MAIL給IT", e);
            }
        }
        finally{
            Date end = new Date();
            logger.info("帳單整批匯入結束 -" + ImportJob.BASIC_DATE_TIME_FORMAT.format(end));
            logger.info("共費時  - " + ((end.getTime() - start.getTime()) / 1000) + " 秒 ");
        }
        
    }
    


    @Override
    public void doCreateReport(Long uploadFilePk, User modifyUser) {
        CreateReportJob reportJob = 
            (CreateReportJob)SpringContextUtil.getBean("createReportJob");
        
        reportJob.setModifyUser(modifyUser);

        Date start = new Date();
        logger.info("產生帳單開始 -" + CreateReportJob.BASIC_DATE_TIME_FORMAT.format(start));
        try {
            reportJob.execute(uploadFilePk);

        } catch (Exception e) {
            CreateReportJob.logger.error(e.getMessage(), e);
            //mail to IT
        }
        finally
        {
            Date end = new Date();
            logger.info("產生帳單結束 -" + ImportJob.BASIC_DATE_TIME_FORMAT.format(end));
            logger.info("共費時  - " + ((end.getTime() - start.getTime()) / 1000) + " 秒 ");

        }

    }
    
    
    public void doBatchCreateReport(List<UploadFile> uploadFiles, User modifyUser)
    {
    	for(Iterator<UploadFile> i=uploadFiles.iterator();i.hasNext();)
    	{
    		UploadFile uploadFile = (UploadFile)i.next();
    		if(uploadFile != null && uploadFile.getUploadFilePk() != null)
    			doCreateReport(uploadFile.getUploadFilePk(), modifyUser);
    	}
    }


    /* (non-Javadoc)
     * @see com.feib.soeasy.service.AsyncService#doPayerFileImport(java.lang.Long)
     */
    @Override
    public void doPayerFileImport(Long uploadFilePk) {
        
       // ImportJob importJob = (ImportJob)SpringContextUtil.getBean("importJob");
        PyerImportJob pyerImportJob = (PyerImportJob)SpringContextUtil.getBean("pyerImportJob");
        
        
        Date start = new Date();
        //有執行
        logger.info("繳款人整批匯入開始 -" + PyerImportJob.BASIC_DATE_TIME_FORMAT.format(start));
        try {
            pyerImportJob.execute(uploadFilePk);           
        }
        catch (Exception e) {
            PyerImportJob.logger.error(e.getMessage(), e);
            try {
                pyerImportJob.exceptionNotice(e, uploadFilePk);
            }
            catch (IOException e1) {
                PyerImportJob.logger.error("完全無法發送MAIL給IT", e);
            }
        }
        finally{
            Date end = new Date();
            logger.info("繳款人整批匯入結束 -" + PyerImportJob.BASIC_DATE_TIME_FORMAT.format(end));
            logger.info("共費時  - " + ((end.getTime() - start.getTime()) / 1000) + " 秒 ");
        }
    }
}
